package com.zhilianbao.leyaogo.ui.fragment.me.myorder;

import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;

import com.bql.baseadapter.recycleView.QuickRcvAdapter;
import com.bql.utils.EventManager;
import com.zhilianbao.leyaogo.R;
import com.zhilianbao.leyaogo.events.LeEventCode;
import com.zhilianbao.leyaogo.http.api.AccountApi;
import com.zhilianbao.leyaogo.http.callback.LoadingViewCallback;
import com.zhilianbao.leyaogo.model.response.UserLoginInfo;
import com.zhilianbao.leyaogo.model.response.me.MyOrderResponse;
import com.zhilianbao.leyaogo.ui.adapter.me.MyOrderAdapter;
import com.zhilianbao.leyaogo.ui.fragment.base.RefreshAndLoadFragment;
import com.zhilianbao.leyaogo.utils.Utils;
import com.zhilianbao.leyaogo.view.decoration.ListLineDecoration;

import org.greenrobot.eventbus.EventBus;

import java.util.List;

import okhttp3.Response;

/**
 * Created by liu on 2016/10/25.
 * 查看订单的基类
 */
public class BaseOrderFragment extends RefreshAndLoadFragment<MyOrderResponse> {
    public MyOrderAdapter mMyOrderAdapter;
    //  用于判断当前页面是属于哪一个,便于EventBus收发消息
    public int currentChild;//0 all,1 waitToPay,2 waitToSend,3 sending,4 finish,

    @Override
    public void onRcvItemClick(RecyclerView.ViewHolder holder, int position) {

    }

    @Override
    protected int getContentViewLayoutID() {
        return R.layout.fragment_swipe_refresh_load_no_tool_bar;
    }

    @Override
    public QuickRcvAdapter<MyOrderResponse> getAdapter() {
        return mMyOrderAdapter;
    }

    @Override
    public RecyclerView.ItemDecoration getItemDecoration() {
        return new ListLineDecoration();
    }

    @Override
    public RecyclerView.LayoutManager getLayoutManager() {
        return new LinearLayoutManager(mActivity);
    }

    @Override
    public void onRefresh() {
        loadDataList(1, true);
    }

    @Override
    public void loadDataList(int curPage, boolean isPullToRefresh) {
        mCurPage = curPage;
        UserLoginInfo userLoginInfo = Utils.getUserLoginInfo();
        long userId = userLoginInfo.getUserId();
        long supplierId = Utils.getSupplierId();
        String keyword = null;
        int type = getType();//请求类型 0全部订单 1待付款 2待发货 3配送中 4待评价 5返修/退换
        int orderType = 0;//订单类型 0 普通订单 1 虚拟订单 2 充值订单 4 团购订单
        AccountApi.getMyOrderList(mActivity, userId, supplierId, type, orderType, keyword, curPage, pageSize, new RefreshAndLoadCallback<List<MyOrderResponse>>(isPullToRefresh) {
            @Override
            public void errorLeftOrEmptyBtnClick(View v) {
                TextView tv = (TextView) v;
                if (tv.getText().toString().trim().equals(emptyBtnText())) {
                    EventBus.getDefault().post(new EventManager(LeEventCode.MSG_CODE_SWITCH_TO_HOME));
                    mActivity.finish();
                } else {
                    loadDataList(1, false);
                }
            }

            @Override
            public void onResultSuccess(List<MyOrderResponse> myOrderResponses, @Nullable Response response, LoadingViewCallback callback) {
                List<MyOrderResponse> list = getList(myOrderResponses);
                handleRefreshAndLoadListData(curPage, callback, list);
            }

            @Override
            public Drawable emptyDrawable() {
                return getResources().getDrawable(R.drawable.img_order_is_empty);
            }

            @Override
            public String emptyContent() {
                return getEmptyContent();
            }

            @Override
            public boolean showEmptyButton() {
                return true;
            }

            @Override
            public String emptyBtnText() {
                return getString(R.string.empty_go);
            }
        });
    }

    public int getType() {
        return 0;
    }

    public String getEmptyContent() {
        return getString(R.string.empty_my_order);
    }

    public List<MyOrderResponse> getList(List<MyOrderResponse> myOrderResponses) {
        return null;
    }

    @Override
    public void onLoadMore() {
        loadDataList(mCurPage, true);
    }

    @Override
    protected boolean lazyLoadMode() {
        return true;
    }

    @Override
    protected void initLazyViews(@Nullable Bundle savedInstanceState) {
        super.initLazyViews(savedInstanceState);
        //确定当前页面属于哪一个页面的方法,必须在new Adapter之前赋值
        initCurrentChild();
        mMyOrderAdapter = new MyOrderAdapter(this, mActivity, mList, currentChild);
    }

    //确定当前页面属于哪一个页面的方法,留给子类复写
    public void initCurrentChild() {

    }

    @Override
    public boolean registerEventBus() {
        return true;
    }

    @Override
    protected void onHandleEvent(EventManager eventManager) {
        super.onHandleEvent(eventManager);
    }

    @Override
    protected void initViewsAndEvents(Bundle savedInstanceState) {

    }

    @Override
    protected void initToolbarHere() {

    }
}
